//
//  CYMeVController.m
//  CreditAddressBook
//
//  Created by Lee on 15/4/25.
//  Copyright (c) 2015年 Lee. All rights reserved.
//

#import "CYMeVController.h"
#import "CYUser.h"
#import "MJExtension.h"
#import "UIButton+WebCache.h"
#import "CYLoginHttpTool.h"
#import "DSNavigationBar.h"

@interface CYMeVController ()<UINavigationControllerDelegate, UINavigationBarDelegate>

@property (weak, nonatomic) IBOutlet UIButton *loginBtn;
@property (weak, nonatomic) IBOutlet UILabel *mineDataLbel;
@property (weak, nonatomic) IBOutlet UIButton *personalBtn;
@property (weak, nonatomic) IBOutlet UIImageView *iconPhotoImageView;

@end

@implementation CYMeVController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self loginStatus];
    self.personalBtn.imageView.contentMode = UIViewContentModeScaleAspectFill;
}

/**
 *  初始化个人中心数据
 */
- (void)loginStatus {
    self.loginBtn.layer.cornerRadius = 4.f;
    self.loginBtn.layer.borderWidth = 1;
    self.loginBtn.layer.borderColor = [CYNaviThemeColor CGColor];
    self.loginBtn.layer.masksToBounds = YES;
    
    //从沙盒中获取数据
    CYUser *user = [CYLoginHttpTool userData].user;
    //若数据不存在，表示用户未登录
    if (user) {
        self.personalBtn.enabled = YES;
        if (user.nickName) {
            self.mineDataLbel.text = user.nickName;
        }else {
            self.mineDataLbel.text = user.phone;
        }
        if (user.headPic && ![user.headPic isEqualToString:@""]) {
            [self setupHeadPicProperty];
            CYLog(@"headPic:%@", user.headPic);
            [self.personalBtn sd_setImageWithURL:[NSURL URLWithString:user.headPic] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"complete_profile_men_sel"]];
        }
        self.loginBtn.hidden = YES;
        self.mineDataLbel.hidden = NO;
        self.iconPhotoImageView.hidden = NO;
    }else {
        self.personalBtn.enabled = NO;
        self.loginBtn.   hidden = NO;
        self.mineDataLbel.hidden = YES;
        self.iconPhotoImageView.hidden = YES;
    }
    
    //个人信息页面用户昵称跟改监听,和个人信息页面用户头像更改监听
    [CYNotificationCenter addObserver:self selector:@selector(nickNameChange:) name:CYNickNameDidChangeNotification object:nil];
    
}

/**
 *  设置头像边框
 */
- (void)setupHeadPicProperty {
    self.personalBtn.imageView.layer.cornerRadius = 40.0;
    self.personalBtn.imageView.layer.masksToBounds = YES;
    self.personalBtn.imageView.layer.borderColor = [CYNaviThemeColor CGColor];
    self.personalBtn.imageView.layer.borderWidth = 2.5;
}

/**
 *  用户没有登录时跳转到登录界面
 */
- (IBAction)login:(id)sender {
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Login" bundle:nil];
    UIViewController *loginViewController = storyboard.instantiateInitialViewController;
    [self presentViewController:loginViewController animated:YES completion:nil];
}

/**
 *  个人信息页面用户昵称更改监听方法
 */
- (void)nickNameChange:(NSNotification *)notification {
    NSString *nickName = notification.userInfo[CYChangedNickName];
    if (nickName) {
        self.mineDataLbel.text = nickName;
    }
    NSString *headPic = notification.userInfo[CYChangedHeadPic];
    if (headPic) {
        [self.personalBtn sd_setImageWithURL:CYURLWithUserService(headPic) forState:UIControlStateSelected placeholderImage:[UIImage imageWithName:@"match_randomPhoneDefaultHead"]];
        [self.personalBtn sd_setImageWithURL:CYURLWithUserService(headPic) forState:UIControlStateNormal placeholderImage:[UIImage imageWithName:@"match_randomPhoneDefaultHead"]];
    }
}

- (IBAction)myLostClick:(id)sender {
    CYAlertView(CYUndeveloped);
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [DSNavigationBar setNavigationBarWithNavigationBar:self.navigationController.navigationBar color:[UIColor clearColor]];
//    [DSNavigationBar setNavigationBarTitleAttributesWithNavigationBar:self.navigationController.navigationBar color:[UIColor blackColor]];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [DSNavigationBar setNavigationBarWithNavigationBar:self.navigationController.navigationBar color:CYNaviThemeColor];
     
//    [DSNavigationBar setNavigationBarTitleAttributesWithNavigationBar:self.navigationController.navigationBar color:CYNaviTitleColor];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)dealloc {
    //移除监听
    [CYNotificationCenter removeObserver:self];
    CYLog(@"个人中心销毁");
}

@end
